package com.fieldchain.ex.fa100.utils.third.face;

import android.content.Context;
import android.os.Build;
import android.widget.TextView;
import android.widget.Toast;
import com.fieldchain.ex.fa100.utils.permission.PermissionManager;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.megvii.faceid.zzplatform.sdk.config.UserConfig;
import com.megvii.faceid.zzplatform.sdk.listener.AccessCallBackListener;
import com.megvii.faceid.zzplatform.sdk.manager.FaceppManager;

public class CheckSelfUtils {
    public static void start(final Context mContext, final String name, final String idCard, final TextView tvNext){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            tvNext.setClickable(false);
            startAudio(mContext, name, idCard, tvNext);
        }else {
            PermissionManager.getInstance().getFaceIdPermission(mContext, new PermissionManager.onPermissionRequest() {
                @Override
                public void isOk() {
                    tvNext.setClickable(false);
                    startAudio(mContext, name, idCard, tvNext);
                }

                @Override
                public void never() {
                    MyToast.show("权限未开启，启动失败");
                }

                @Override
                public void refuse() {
                    MyToast.show("权限未开启，启动失败");
                }
            });
        }

    }

    private static void startAudio(final Context mContext, final String name, final String idCard, final TextView tvNext) {
        if (!CheckIDCardUtil.checkName(name)) {
            tvNext.setClickable(true);
            Toast.makeText(mContext, "请正确填写名字信息", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!CheckIDCardUtil.checkNum(idCard)) {
            tvNext.setClickable(true);
            Toast.makeText(mContext, "请正确填写身份证号信息", Toast.LENGTH_SHORT).show();
            return;
        }
        String apiKey = "cTxkCBaRrLnpZ4gMTuf892PWo619IaAS";
        String secret = "Gff1K-efyhB0jRaICX95TF7XE0q7tlLR";
        long currtTime = System.currentTimeMillis() / 1000;
        long expireTime = (System.currentTimeMillis() + 60 * 60 * 100) / 1000;
        String sign = GenerateSign.appSign(apiKey, secret, currtTime, expireTime);
        sign = sign.replaceAll("[\\s*\t\n\r]", "");
        String signVersion = "hmac_sha1";
        UserConfig userConfig = new UserConfig();
        userConfig.setIdcardName(name);
        userConfig.setIdcardNumber(idCard);
        userConfig.setNotifyUrl("http://dev.alicms.com:9000/auth/callback");
        FaceppManager.getInstance(mContext).init(sign, signVersion, userConfig, new AccessCallBackListener() {
            @Override
            public void onSuccess(String responseBody) {
                FaceppManager.getInstance(mContext).startDetect();
                tvNext.setClickable(true);
            }

            @Override
            public void onFailure(int statusCode) {
                tvNext.setClickable(true);
                Toast.makeText(mContext, "Failed...", Toast.LENGTH_SHORT).show();
            }
        });
    }




}
